﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using SCRUM.Core.MembershipProviders;
using SCRUM.Core.EmailProvider;


/// <summary>
/// Widget for creating user.
/// It serves admin for quick user(s) creating. Only inevitable items are presented. 
/// </summary>
public partial class CreateUser : WidgetBase
{
    /// <summary>
    /// Gets the name of the widget. It must be exactly the same as the folder that contains the widget.
    /// </summary>
    /// <value></value>
    public override string Name
    {
        get 
        { 
            return "CreateUser"; 
        }
    }

    /// <summary>
    /// Gets the title. Title is shown in the header of the widget container.
    /// </summary>
    /// <value>The title.</value>
    public override string Title
    {
        get 
        { 
            return (string)GetLocalResourceObject("Title"); 
        }
    }
    /// <summary>
    /// This method works as a substitute for Page_Load. You should use this method for
    /// data binding etc. instead of Page_Load.
    /// </summary>
    public override void LoadWidget()
    {

    }

    /// <summary>
    /// Shows the edit.
    /// </summary>
    public override void ShowEdit()
    {
       
    }

    /// <summary>
    /// Hides the edit.
    /// </summary>
    public override void HideEdit()
    {
        
    }

    /// <summary>
    /// Gets a value indicating whether this instance is editable.
    /// </summary>
    /// <value>
    /// 	<c>true</c> if this instance is editable; otherwise, <c>false</c>.
    /// </value>
    public override bool IsEditable
    {
        get 
        { 
            return false; 
        }
    }

    /// <summary>
    /// Generates the random password.
    /// </summary>
    /// <param name="sender">The sender.</param>
    /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
    protected void GeneratePassword(object sender, EventArgs e)
    {
        int passwordLength = 8;
        string allowedChars = "abcdefghijkmnopqrstuvwxyzABCDEFGHJKLMNOPQRSTUVWXYZ0123456789!@$?_-";
        char[] chars = new char[passwordLength];
        Random rd = new Random();

        for (int i = 0; i < passwordLength; i++)
        {
            chars[i] = allowedChars[rd.Next(0, allowedChars.Length)];
        }

        TextBoxPassword.Text = new string(chars);
        TextBoxEmail.Focus();
    }

    /// <summary>
    /// Clears the form.
    /// </summary>
    /// <param name="sender">The sender.</param>
    /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
    protected void ClearForm(object sender, EventArgs e)
    {
        TextBoxLogin.Text = String.Empty;
        TextBoxPassword.Text = String.Empty;
        TextBoxEmail.Text = String.Empty;
        CheckBoxSendEmail.Checked = true;
        LabelResult.Visible = false;
        LabelRequiredTeam.Visible = false;
        LabelSendingStatus.Visible = false;
        LabelRequiredTeam.Visible = false;
        TextBoxLogin.Focus();
        TeamSelection.Clear();
    }


    /// <summary>
    /// Creates the user.
    /// </summary>
    /// <param name="sender">The sender.</param>
    /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
    protected void CreateNewUser(object sender, EventArgs e)
    {
        if (!Page.IsValid)
        {
            return;
        }

        if(TeamSelection.SelectedTeam == null)
        {
            LabelRequiredTeam.Text = String.Format((string)GetLocalResourceObject("RequiredTeam"));
            LabelRequiredTeam.ForeColor = System.Drawing.Color.Red;
            return;
        }
        System.Web.Security.MembershipCreateStatus status;

        MembershipProviderFactory.MembershipProvider.CreateNewUser(TextBoxLogin.Text, TextBoxPassword.Text, TextBoxEmail.Text, string.Empty, string.Empty, true, 1, TextBoxLogin.Text, 1, TeamSelection.SelectedTeam.ID, 1, out status);
        if (status == System.Web.Security.MembershipCreateStatus.Success)
        {
            if (CheckBoxSendEmail.Checked)
            {
                sendEmail();
            }
            LabelResult.Text = String.Format((string)GetLocalResourceObject("Success"), TextBoxLogin.Text);
            LabelResult.ForeColor = System.Drawing.Color.Green;
        }
        else
        {
            LabelResult.Text = status.ToString();//String.Format((string)GetLocalResourceObject("Fail"), TextBoxUserName.Text);
            LabelResult.ForeColor = System.Drawing.Color.Red;
        }
        
        ClearForm(sender, e);
        LabelResult.Visible = true;
        LabelSendingStatus.Visible = true;
        LabelRequiredTeam.Visible = true;
    }

    /// <summary>
    /// Sends the email with login and password information.
    /// </summary>
    private void sendEmail()
    {
        string body;

        body = (string)GetLocalResourceObject("MailBodyPart1") + TextBoxLogin.Text;
        body += (string)GetLocalResourceObject("MailBodyPart2") + TextBoxPassword.Text;

        LabelSendingStatus.Text = (string)GetLocalResourceObject("Sending");
        
        EmailProvider.SendEmail(TextBoxEmail.Text, (string)GetLocalResourceObject("MailSubject"), body);

        LabelSendingStatus.Text = (string)GetLocalResourceObject("SentEmailPart") + TextBoxEmail.Text;
    }
                  
}
